package com.ruoyi.project.business.controller;



import java.util.List;

import com.ruoyi.common.utils.Encryption;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.business.domain.TgxsUser;
import com.ruoyi.project.business.service.ITgxsUserService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 用户Controller
 * 
 * @author ruoyi
 * @date 2020-10-22
 */
@RestController
@RequestMapping("/business/user")
public class TgxsUserController extends BaseController
{
    @Autowired
    private ITgxsUserService tgxsUserService;

    /**
     * 查询用户列表
     */
    @PreAuthorize("@ss.hasPermi('business:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(TgxsUser tgxsUser)
    {
        startPage();
        List<TgxsUser> list = tgxsUserService.selectTgxsUserList(tgxsUser);
        for (int i=0;i<list.size();i++){
           list.get(i).setCapital(Encryption.decrypt(list.get(i).getCapital()));
        }
        return getDataTable(list);
    }

    /**
     * 导出用户列表
     */
    @PreAuthorize("@ss.hasPermi('business:user:export')")
    @Log(title = "用户", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TgxsUser tgxsUser)
    {
        List<TgxsUser> list = tgxsUserService.selectTgxsUserList(tgxsUser);
        ExcelUtil<TgxsUser> util = new ExcelUtil<TgxsUser>(TgxsUser.class);
        return util.exportExcel(list, "user");
    }

    /**
     * 获取用户详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:user:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        TgxsUser tgxsUser=tgxsUserService.selectTgxsUserById(id);
        tgxsUser.setCapital(Encryption.decrypt(tgxsUser.getCapital()));
        return AjaxResult.success(tgxsUser);
    }

    /**
     * 新增用户
     */
    @PreAuthorize("@ss.hasPermi('business:user:add')")
    @Log(title = "用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TgxsUser tgxsUser)
    {
        return toAjax(tgxsUserService.insertTgxsUser(tgxsUser));
    }

    /**
     * 修改用户
     */
    @PreAuthorize("@ss.hasPermi('business:user:edit')")
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TgxsUser tgxsUser)
    {
        if(tgxsUser.getCapital()!=null||tgxsUser.getCapital()!=""){
            tgxsUser.setCapital(Encryption.encrypt(tgxsUser.getCapital()));
        }
        return toAjax(tgxsUserService.updateTgxsUser(tgxsUser));
    }

    /**
     * 删除用户
     */
    @PreAuthorize("@ss.hasPermi('business:user:remove')")
    @Log(title = "用户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tgxsUserService.deleteTgxsUserByIds(ids));
    }
}